package com.teamsapp.article.repository;

import com.teamsapp.article.model.Article;
import com.teamsapp.article.model.ArticleStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface ArticleRepository extends JpaRepository<Article, String> {
    // 根据状态查询文章（分页）
    Page<Article> findByStatus(ArticleStatus status, Pageable pageable);

    // 查询所有草稿文章和没有草稿版本的已发布文章
    @Query("SELECT a FROM Article a WHERE a.status = 'DRAFT' OR " +
           "(a.status = 'PUBLISHED' AND NOT EXISTS (SELECT 1 FROM Article d WHERE d.originalId = a.id))")
    Page<Article> findLatestVersions(Pageable pageable);
}